#pragma once

#include "RenderableEntity.h"
namespace Graphics
{
	class Buffer;
	class Texture;
}

namespace Graphics
{
const int TEXTURE_REPEAT = 8;

class Terrain : public RenderableEntity
{
	struct VertexType
	{
		D3DXVECTOR3 position;
		D3DXVECTOR2 texture;
		D3DXVECTOR3 normal;
	};

	struct HeightMapType
	{
		float x, y, z;
		float tu, tv;
		float nx, ny, nz;
	};

	struct VectorType 
	{ 
		float x, y, z;
	};

public:
	Terrain(void);
	virtual ~Terrain(void);
	virtual void Initialize(const char* heightMapName, const char* textureName);
	virtual void Deinitialize();
	virtual void Render();
	virtual int GetIndexCount();
	Texture* GetTexture();

private:
	bool CalculateNormals();
	void CalculateTextureCoordinates();

	bool InitializeBuffers();
	void DeinitializeBuffers();
	void RenderBuffers();

	bool LoadHeightMap(const char* heightMapName);
	void NormalizeHeightMap();
	void DeinitializeHeightMap();

private:
	int mWidth; 
	int mHeight;
	int mVertexCount;
	int mIndexCount;
	Buffer* mVertexBuffer;
	Buffer* mIndexBuffer;
	HeightMapType* mHeightMap;
	Texture* mTexture;
};
}